#include "widget_setup_network.h"



network_setup_widget::network_setup_widget( QWidget* parent, Qt::WindowFlags flags ): setup_widget( parent, flags )
{	
	layout = new QVBoxLayout( this );


	QWidget* title_w = new QWidget( this );
	QHBoxLayout* title_w_l = new QHBoxLayout( title_w );

	QLabel* title_l = new QLabel( title_w );
	title_l->setTextFormat( Qt::RichText );
	title_l->setText( "<b><font size=8>Network Game Setup</font></b>" );
	title_w_l->addWidget( title_l );

	next_b = new QPushButton( "NEXT", title_w );
	next_b->setFixedWidth( 150 );
	next_b->setEnabled( false );
	title_w_l->addWidget( next_b );
	connect( next_b, SIGNAL( clicked() ), this, SLOT( next() ) );

	title_w->setLayout( title_w_l );
	layout->addWidget( title_w );


	layout->addSpacing( 20 );


	QLabel* set_l = new QLabel( this );
	set_l->setTextFormat( Qt::RichText );
	set_l->setText( "<b><font size=6>Setting of new player</font></b>" );
	layout->addWidget( set_l );


	QWidget* set_w = new QWidget( this );
	QHBoxLayout* set_w_l = new QHBoxLayout( set_w );
	set_w_l->setSpacing( 20 );

	QLabel* name_l = new QLabel( set_w );
	name_l->setTextFormat( Qt::RichText );
	name_l->setText( "<font size=5>Name:</font></b>" );
	set_w_l->addWidget( name_l );

	name_i = new QLineEdit( set_w );
	name_i->setFixedWidth( 250 );
	set_w_l->addWidget( name_i );	

	QLabel* type_l = new QLabel( set_w );
	type_l->setTextFormat( Qt::RichText );
	type_l->setText( "<font size=5>Type:</font></b>" );
	set_w_l->addWidget( type_l );

	type_i = new QComboBox( set_w );
	type_i->addItem( "Human" );
	type_i->addItem( "AI" );
	set_w_l->addWidget( type_i );
	
	set_b = new QPushButton( "Set player", set_w );
	set_w_l->addWidget( set_b );
	connect( set_b, SIGNAL( clicked() ), this, SLOT( set_player() ) );

	set_w->setLayout( set_w_l );
	layout->addWidget( set_w );


	QWidget* algorithm_w = new QWidget( this );
	QHBoxLayout* algorithm_w_l = new QHBoxLayout( algorithm_w );
	algorithm_w_l->setSpacing( 20 );

	QLabel* algorithm_l = new QLabel( algorithm_w );
	algorithm_l->setTextFormat( Qt::RichText );
	algorithm_l->setText( "<font size=5>AI Algorithm:</font></b>" );
	algorithm_w_l->addWidget( algorithm_l );

	algorithm_i = new QComboBox( algorithm_w );
	for( map< string, algorithm* >::iterator it = ai_algorithms::get_instance()->algorithm_list.begin(); it != ai_algorithms::get_instance()->algorithm_list.end(); ++it )
	{
		//skip replay (not real algorithm)
		if ( it->first == "Replay" )
			continue;
		algorithm_i->addItem( ( it->first ).c_str() );
	}
	algorithm_i->setEnabled( false );
	algorithm_w_l->addWidget( algorithm_i );	
	
	algorithm_b = new QPushButton( "Set algorithm", algorithm_w );
	algorithm_b->setEnabled( false );
	algorithm_w_l->addWidget( algorithm_b );
	connect( algorithm_b, SIGNAL( clicked() ), this, SLOT( set_algorithm() ) );

	algorithm_w->setLayout( algorithm_w_l );
	layout->addWidget( algorithm_w );


	QLabel* connect_l = new QLabel( this );
	connect_l->setTextFormat( Qt::RichText );
	connect_l->setText( "<b><font size=6>Connecting to the server</font></b>" );
	layout->addWidget( connect_l );


	QWidget* connect_w = new QWidget( this );
	QHBoxLayout* connect_w_l = new QHBoxLayout( connect_w );

	QLabel* address_l = new QLabel( connect_w );
	address_l->setTextFormat( Qt::RichText );
	address_l->setText( "<font size=5>IP Address:</font></b>" );
	connect_w_l->addWidget( address_l );

	address_i = new QLineEdit( connect_w );
	address_i->setFixedWidth( 250 );
	address_i->setText( "127.0.0.1" );
	connect_w_l->addWidget( address_i );

	QLabel* port_l = new QLabel( connect_w );
	port_l->setTextFormat( Qt::RichText );
	port_l->setText( "<font size=5>Port:</font></b>" );
	connect_w_l->addWidget( port_l );

	port_i = new QLineEdit( connect_w );
	port_i->setFixedWidth( 70 );
	port_i->setText( "1234" );
	connect_w_l->addWidget( port_i );

	connect_b = new QPushButton( "Connect", connect_w );
	connect_b->setEnabled( false );
	connect_b->setFixedWidth( 150 );
	connect_w_l->addWidget( connect_b );
	connect( connect_b, SIGNAL( clicked() ), this, SLOT( connect_server() ) );

	connect_w->setLayout( connect_w_l );
	layout->addWidget( connect_w );


	status_l = new QLabel();
	status_l->setTextFormat( Qt::RichText );
	layout->addWidget( status_l );


	this->setLayout( layout );
	//this->setMaximumSize( 653, 327 );
};



void network_setup_widget::set_player()
{
	string nm = name_i->text().remove( ':' ).remove( '\\' ).remove( '=' ).toStdString();
	
	if ( nm == "" )
	{
		nm = "Local Player";
		name_i->setText( nm.c_str() );
	}
	unsigned int typ;
	if ( type_i->currentText() == "AI" )
	{
		algorithm_b->setEnabled( true );
		algorithm_i->setEnabled( true );
		typ = 1;
	}
	else
	{
		connect_b->setEnabled( true );
		typ = 0;
	}

	network_client::get_instance()->set_player( nm, typ );

	set_b->setEnabled( false );
	name_i->setReadOnly( true );
	type_i->setEnabled( false );
};



void network_setup_widget::connect_server()
{
	istringstream istr( port_i->text().toStdString() );
	int port;
	istr >> port;
	network_client::get_instance()->connect_server( address_i->text().toStdString(), port );
	connect_b->setText( "Connecting..." );
	connect_b->setEnabled( false );
	port_i->setReadOnly( true );
	address_i->setReadOnly( true );
};



void network_setup_widget::show_status( const string& s )
{
	set_status( s );
	if ( network_client::get_instance()->client->state() == QAbstractSocket::UnconnectedState )
	{
		connect_b->setText( "Connect" );
		connect_b->setEnabled( true );
		port_i->setReadOnly( false );
		address_i->setReadOnly( false );
	}
	if ( network_client::get_instance()->client->state() == QAbstractSocket::ConnectedState )
	{
		set_status( "Waiting for other players..." );
		connect_b->setText( "Connected!" );
		connect_b->setEnabled( false );
	}
};



void network_setup_widget::set_status( const string& s )
{
	ostringstream sout;
	sout << "<b><font size=4>" << s << "</font></b>";	
	status_l->setText( sout.str().c_str() );
};



void network_setup_widget::set_algorithm()
{
	network_client::get_instance()->set_players_algorithm( algorithm_i->currentText().toStdString() );
	connect_b->setEnabled( true );
	algorithm_b->setEnabled( false );
	algorithm_i->setEnabled( false );
};



void network_setup_widget::next()
{
	//only wait for other players
};